package jp.heartdokyun.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import jp.heartdokyun.R;
import jp.heartdokyun.activity.GameActivity;
import jp.heartdokyun.model.Level;
import jp.heartdokyun.util.Utils;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class StageSelectionAdapter extends BaseAdapter {
	private Context mContext;
	private ArrayList<Level> mListLevel;
	private Intent itGame;
	private SoundPool mSoundPool;
	private HashMap<Integer, Integer> soundPoolMap;

	public StageSelectionAdapter(Context ctx, ArrayList<Level> listLevel) {
		this.mContext = ctx;
		this.mListLevel = listLevel;
		itGame = new Intent(this.mContext, GameActivity.class);
		itGame.putExtra("CHALLENGE_TYPE", ((Activity) this.mContext)
				.getIntent().getStringExtra("CHALLENGE_TYPE"));
		itGame.putExtra("CHALLENGE_TYPE_VALUE", ((Activity) this.mContext)
				.getIntent().getStringExtra("CHALLENGE_TYPE_VALUE"));

		initSounds();
	}

	@Override
	public int getCount() {
		return mListLevel.size();
	}

	@Override
	public Level getItem(int index) {
		return mListLevel.get(index);
	}

	@Override
	public long getItemId(int index) {
		long tmp = 0;
		String level = mListLevel.get(index).getLevel();
		if (!level.equals("")) {
			tmp = Long.parseLong(level);
		}

		return tmp;
	}

	@Override
	public View getView(final int index, View convertView, ViewGroup parent) {
		if (convertView == null) {
			LayoutInflater vi = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = vi.inflate(R.layout.stage_selection_item, null);
		}
		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!mListLevel.get(index).getTitle().trim().equals("")) {
					playSound(1);

					itGame.putExtra("CHALLENGE_TYPE", ((Activity) mContext)
							.getIntent().getStringExtra("CHALLENGE_TYPE"));
					itGame.putExtra(
							"CHALLENGE_TYPE_VALUE",
							((Activity) mContext).getIntent().getStringExtra(
									"CHALLENGE_TYPE_VALUE"));
					itGame.putExtra("LEVEL", mListLevel.get(index).getLevel());
					mContext.startActivity(itGame);
				}
			}
		});
		TextView   tvTime;
		ImageView tvLevel,tvTitle,tvRank ;
		tvLevel = (ImageView) convertView.findViewById(R.id.tv_ssi_level);
		tvTitle = (ImageView) convertView.findViewById(R.id.tv_ssi_title);
		tvRank = (ImageView) convertView.findViewById(R.id.tv_ssi_rank);
		tvTime = (TextView) convertView.findViewById(R.id.tv_ssi_time);
		
//		tvLevel.setImageResource(String.valueOf(mListLevel.get(index).getLevelName()));
//		tvTitle.setText(mListLevel.get(index).getTitle());
//		tvRank.setText(mListLevel.get(index).getRank());

		tvLevel.setImageResource(mContext.getResources().getIdentifier(
				"level_" + (index + 1), "drawable", "jp.heartdokyun"));
		if (!mListLevel.get(index).getTitle().trim().equals("")) {
			tvTitle.setImageResource(mContext.getResources().getIdentifier(
					"text_title_" + mListLevel.get(index).getType() +  "_" +  (index + 1),
					"drawable", "jp.heartdokyun"));
			tvTitle.setBackgroundColor(0xFFFFFFFF);
		} else {
			tvTitle.setImageResource(mContext.getResources().getIdentifier(
					"text_close", "drawable", "jp.heartdokyun"));
		}

		if (mListLevel.get(index).getRank().equals("A")) {
			tvRank.setImageResource(mContext.getResources().getIdentifier(
					"rank_a", "drawable", "jp.heartdokyun"));
			tvRank.setBackgroundColor(0xFFFFFFFF);
		} else if (mListLevel.get(index).getRank().equals("B")) {
			tvRank.setImageResource(mContext.getResources().getIdentifier(
					"rank_b", "drawable", "jp.heartdokyun"));
			tvRank.setBackgroundColor(0xFFFFFFFF);
		} else if (mListLevel.get(index).getRank().equals("C")) {
			tvRank.setImageResource(mContext.getResources().getIdentifier(
					"rank_c", "drawable", "jp.heartdokyun"));
			tvRank.setBackgroundColor(0xFFFFFFFF);
		} else {
			tvRank.setImageResource(mContext.getResources().getIdentifier(
					"no_rank", "drawable", "jp.heartdokyun"));
			if (!mListLevel.get(index).getTitle().trim().equals("")) {
				tvRank.setBackgroundColor(0xFFFFFFFF);

			}else{
				tvRank.setBackgroundColor(0x4C4C4C4C);
			}
			
		}

		try {
			if (mListLevel.get(index).getTime().equals("")) {
				tvTime.setText("");
				
			} else {
				// tvTime.setText("00:"
				// + Integer.parseInt(mListLevel.get(index).getTime())
				// / 1000);
//				int displayTime = 30000- Integer.parseInt(mListLevel.get(index).getTime());
				tvTime.setText(getDisplayTime(mListLevel.get(index).getTime()));

			}
			if (!mListLevel.get(index).getTitle().trim().equals("")) {
				tvTime.setBackgroundColor(0XFFFFFFFF);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}

	private void initSounds() {
		mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
		soundPoolMap = new HashMap<Integer, Integer>();
		soundPoolMap.put(1, mSoundPool.load(
				this.mContext.getApplicationContext(), R.raw.go, 1));

		// soundPoolMap.put(2,
		// mSoundPool.load(getApplicationContext(), R.raw.loop_33, 1));
	}

	public void playSound(int sound) {
		AudioManager mgr = (AudioManager) this.mContext.getApplicationContext()
				.getSystemService(Context.AUDIO_SERVICE);
//		float streamVolumeCurrent = mgr
//				.getStreamVolume(AudioManager.STREAM_MUSIC);
		// float streamVolumeMax = mgr
		// .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		// float volume = streamVolumeCurrent / streamVolumeMax;
		float volume = Utils.getVoiceVolume(mContext.getApplicationContext());
		
		mSoundPool.play(soundPoolMap.get(sound), volume,
				volume, 1, 0, 1f);
	}

	private String getDisplayTime(String time) {
		int playedTime = Integer.parseInt(time);

		int second = playedTime / 1000;
		int miliSecond = playedTime % 1000;

		return Integer.toString(second) + "\""
				+ Integer.toString(miliSecond).substring(0, 1);
	}

}
